/*
#####################################################################
####################	   T R I B U I T 		#####################
#####################################################################

Copyright (C) 2010  Christoph Schnack, Berlin (Germany)

This file is part of Tribuit.

    Tribuit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Tribuit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tribuit.  If not, see <http://www.gnu.org/licenses/>.


########################################################################
*/
package PictureDescription;

import javax.swing.*;

import GuiTools.TribuitFrame;
import PDPictureEditor.PictureEditorDialog;
import Settings.*;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.*;
/**
 * Panel showing a preview frame and a list of PictureUnit names. 
 * @author Christoph Schnack
 *
 */
public class PictureSelectionPanel extends JPanel {
	HashMap<String,Image> DATA;
	JList list;
	PictureSelectionPanel panel;
	PreviewPanel previewPanel;
	TribuitFrame pcmFrame;
	PictureVocabularyManageDialog pvmd;
	Language LANG;
	Settings SET;
	
	public PictureSelectionPanel(Settings set,PictureVocabularyManageDialog pvm,TribuitFrame frame,HashMap<String,Image> data){
		super();
		SET = set;
		LANG = set.getLanguage();
		pcmFrame = frame;
		pvmd = pvm;
		DATA=data;
		panel=this;
		buildPanel();
		if(!data.isEmpty()){
			String[] keys = data.keySet().toArray(new String[]{});
			for(String k : keys){
				previewPanel.prepareImage(data.get(k),previewPanel);
			}
		}
	}
	
	public void buildPanel(){
		panel.setPreferredSize(new Dimension(450,300));
		list=new JList();
		list.addMouseListener(new MouseAdapter(){
			public void mouseClicked(MouseEvent e) {
				if(!DATA.isEmpty()){
					int clicks = e.getClickCount();
					String selection = getSelection();
					if(selection!=null){
						previewPanel.setImage(DATA.get(selection));
						if(clicks==2){
							pvmd.openEditDialog(selection);
						}
					}
				}
			}
			
		});
		if(!DATA.isEmpty())list.setListData(DATA.keySet().toArray());
		JScrollPane jsp = new JScrollPane(list);
		jsp.setPreferredSize(new Dimension(150,300));
		panel.setLayout(new BorderLayout());
		previewPanel = new PreviewPanel();
		panel.add(BorderLayout.EAST,jsp);
		panel.add(BorderLayout.CENTER,previewPanel);
	}
	
	public String getSelection(){
		return (String) list.getSelectedValue();
	}
	
	public void removeFromList(String arg){
		DATA.remove(arg);
		if(!DATA.isEmpty()){
			list.setListData(DATA.keySet().toArray());
		}
		else list.setListData(new Object[]{});
		list.clearSelection();
		previewPanel.setImage(null);
	}
	
	class PreviewPanel extends JPanel{
		Image image;
		public PreviewPanel(){
			super();
			this.setBorder(BorderFactory.createTitledBorder(LANG.getPreview()+":"));
			setPreferredSize(new Dimension(300,300));
		}
		
		public void setImage(Image img){
			image = img;
			this.repaint();
		}
		
		public void paintComponent(Graphics g){
			Graphics2D g2 = (Graphics2D)g;
			g2.clearRect(0,0, 300, 300);
			if(image!=null)g2.drawImage(image,50,50,200,200,this);
		}
	}
	
}
